package com.video.h264;
public class Data {
//	int iMessage;
//	char userID[40];
//	unsigned int ip;
//	unsigned short mobilePort;
//	char Reserve[6];
	
	static int iMessage;
	static byte[] userID = new byte [40];
	static byte[]ip = new byte[4];//int ip;
	static byte[]mobilePort = new byte[2];//short mobilePort;
	static byte[] Reserve = new byte[6];
	
	/*
	typedef struct Data
	{
		int iMessage;4
		char userID[40];40
		unsigned int ip;4
		unsigned short mobilePort;2
		char Reserve[6];6
	};
	
	 (int)CSocket 
	{ 
		int sockfd;	
		if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
		{ 
			perror("socket"); 
			exit(errno); 
		} 
		return sockfd; 
	} 
	 (BOOL)ConnectToServer:(char*)addr port:(int)port 
	{ 
		struct hostent * hp;
		hp = gethostbyname(addr);
		if(hp == NULL)
		{
			return NO;
		}

	    memset(&sock_addr, 0, sizeof(sock_addr));
		memcpy(&sock_addr.sin_addr.s_addr, hp->h_addr_list[0],hp->h_length);	
		
		NSString *s;
		s = [NSString stringWithCString:addr];
		sock_addr.sin_family = AF_INET; 
		//sock_addr.sin_addr.s_addr = inet_addr([s UTF8String]); 
		sock_addr.sin_port = htons(port); 
		bzero(&(sock_addr.sin_zero), 8); 
		int conn = connect(sockfd, (struct sockaddr*)&sock_addr, sizeof(struct sockaddr)); 
		NSLog(@"Connect error no is %d:",conn); 
		return conn == 0; 
	} 

	(bool) isfinishgetip
	{
		bool bret = false;
		//
		char *ip, *port;
		ip = NULL;
		port = NULL;
		ip = [self getServerSetsCType:2];
		port = [self getServerSetsCType:1];
		if (strlen(ip)> 7 ) {
			bret = true;
		}
		return bret;
	}

 (char *) inetfint:(unsigned int )ip
	{
		char strip[32] = {0};
		unsigned char ip1, ip2, ip3, ip4;
		ip4 = (ip>>24);
		ip3 = ((ip>>16)& 0x0ff);
		ip2 = ((ip>>8)&0x0ff);
		ip1 = ip&0x0ff;
		sprintf(strip, "%d.%d.%d.%d", ip1, ip2, ip3, ip4);
		return strip;
	}

	 (bool) getipforid:(char*)url
	{
		bool bret = false;
		sockfd = [self CSocket];
		bret =[self ConnectToServer:url port:DVRSVRPORT];
		
		//	bret =[self ConnectToServer:@"59.33.82.51" port:MYPORT];
		if (bret) {
			struct Data d,dd;
			int iret;
			int nlen;
			
			nlen = sizeof(Data);
			
			memset(&d, 0, nlen);
			memset(&dd, 0, nlen);
			d.iMessage = 0x40;
			
			char *url;
			url = [self getServerSetsCType:0];
			strcpy(d.userID, url);
			
			iret = send(sockfd, &d, nlen, 0);
			if(iret == nlen)
			{
				iret = recv(sockfd, &dd, nlen, 0);
				if (iret == nlen
					&& dd.iMessage == 0x24) {
					char szip[32] ;
					strcpy(szip, [self inetfint:dd.ip]);
					NSString *strip = [NSString stringWithCString:szip];
					char szport[32] ;
					if(dd.mobilePort == 0)
						dd.mobilePort =10510;
					
					sprintf(szport, "%d", dd.mobilePort);
					
					[self setServerSets:2 text:strip];
					[self setServerSets:1 text:[NSString stringWithCString:szport]];
					bret = true;
				}
				else {
					bret = false;
				}

			}
		}
		close(sockfd);
		
		return bret;
	}

	 (bool) getactiveloginserver
	{
		bool bret;
		bret = [self getipforid:DVRSVRURL1];
		if (bret) {
			return bret;
		}
		bret = [self getipforid:DVRSVRURL2];
		if (bret) {
			return bret;
		}
		bret = [self getipforid:DVRSVRURL3];
		
		return bret;

	}*/
	 
}
